package cn.com.css.taxInspection.workflow.controller;

import cn.com.css.taxInspection.attention.entity.CaseAjxxRelation;
import cn.com.css.taxInspection.gt3.entity.DmJcAjztEntity;
import cn.com.css.taxInspection.gt3.service.DmGySwryService;
import cn.com.css.taxInspection.gt3.service.DmJcAjztService;
import cn.com.css.taxInspection.workflow.entity.*;
import cn.com.css.taxInspection.workflow.mapper.WorkflowLogMapper;
import cn.com.css.taxInspection.workflow.service.*;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.com.css.taxInspection.common.aop.annotation.LogAnnotation;
import cn.com.css.taxInspection.common.constants.Constants;
import cn.com.css.taxInspection.common.result.DataResult;
import cn.com.css.taxInspection.gt3.entity.DmGySwjgEntity;
import cn.com.css.taxInspection.gt3.entity.JcAjxxEntity;
import cn.com.css.taxInspection.gt3.service.DmGySwjgService;
import cn.com.css.taxInspection.gt3.service.JcAjxxService;
import cn.com.css.taxInspection.system.entity.SysUser;
import cn.com.css.taxInspection.system.service.UserService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import cn.com.css.taxInspection.workflow.constants.WorkflowConstants;
import cn.com.css.taxInspection.workflow.vo.excel.WorkflowLogExcel;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("/workflow/log")
public class WorkflowLogController {

    @Resource
    private WorkflowLogService workflowLogService;
    @Resource
    private GzddAjContentService gzddAjContentService;
    @Resource
    private GzddworkService gzddworkService;
    @Resource
    private GzddmbService gzddmbService;
    @Resource
    private WorkflowLogSurveyService workflowLogSurveyService;
    @Resource
    private WorkflowLogSurveyFeedbackService workflowLogSurveyFeedbackService;
    @Resource
    private GzddmbFeedService gzddmbFeedService;
    @Resource
    private JcAjxxService jcAjxxService;
    @Resource
    private HttpSessionService httpSessionService;
    @Resource
    private DmGySwjgService dmGySwjgService;
    @Resource
    private UserService userService;
    @Resource
    private DmJcAjztService dmJcAjztService;
    @Resource
    private DmGySwryService dmGySwryService;
    @Resource
    private WorkflowLogConfirmService workflowLogConfirmService;
    @Resource
    private WorkflowLogMapper workflowLogMapper;
    @Value("${file.path}")
    private String filePath;
    @Value("${file.url}")
    private String fileUrl;

    //打印日志
    private static final Logger logger = LoggerFactory.getLogger(WorkflowLogController.class);

    @PostMapping("/deletelog/{id}")
    @ApiOperation(value = "删除")
    @LogAnnotation(title = "工作日志", action = "删除")
    @DS("oracle")
    public DataResult deletelog(@PathVariable("id") String id) {
        // 级联删除
        LambdaQueryWrapper<WorkflowLogConfirmEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkflowLogConfirmEntity::getLogId, id);
        List<WorkflowLogConfirmEntity> list = workflowLogConfirmService.list(queryWrapper);
        for (WorkflowLogConfirmEntity workflowLogConfirm : list) {
            workflowLogConfirmService.removeById(workflowLogConfirm.getId());
        }
        workflowLogService.removeById(id);
        return DataResult.success();
    }

    @PostMapping("/startGzdd/{id}")
    @ApiOperation(value = "工作调度-启动调度")
    @LogAnnotation(title = "工作调度", action = "启动调度")
    @DS("oracle")
    public DataResult startGzdd(@PathVariable("id") String id) {
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkflowLogEntity::getId, id);
        WorkflowLogEntity workflowLogEntity = workflowLogService.getOne(queryWrapper);
        workflowLogEntity.setType(Constants.GZDD_DD_PUSH);
        workflowLogService.updateById(workflowLogEntity);
        return DataResult.success();
    }


    @PostMapping("/deleteGzdd/{id}")
    @ApiOperation(value = "工作调度-删除工作调度")
    @LogAnnotation(title = "工作日志", action = "删除工作调度")
    @DS("oracle")
    public DataResult deleteGzdd(@PathVariable("id") String id) {
        // 级联删除
        LambdaQueryWrapper<WorkflowLogConfirmEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkflowLogConfirmEntity::getLogId, id);
        List<WorkflowLogConfirmEntity> list = workflowLogConfirmService.list(queryWrapper);
        for (WorkflowLogConfirmEntity workflowLogConfirm : list) {
            workflowLogConfirmService.removeById(workflowLogConfirm.getId());
        }
        workflowLogService.deleteDataById(id);
        gzddAjContentService.remove(new LambdaQueryWrapper<GzddAjContentEntity>().eq(GzddAjContentEntity::getDduuid, id));
        return DataResult.success();
    }


    @PostMapping("/deleteGzddLog/{id}")
    @ApiOperation(value = "工作调度-删除工作调度")
    @LogAnnotation(title = "工作日志", action = "删除工作调度")
    @DS("oracle")
    public DataResult deleteGzddLog(@PathVariable("id") String id) {
        // 级联删除
        LambdaQueryWrapper<WorkflowLogConfirmEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkflowLogConfirmEntity::getLogId, id);
        List<WorkflowLogConfirmEntity> list = workflowLogConfirmService.list(queryWrapper);
        for (WorkflowLogConfirmEntity workflowLogConfirm : list) {
            workflowLogConfirmService.removeById(workflowLogConfirm.getId());
        }
        workflowLogService.deleteDataById(id);
        gzddAjContentService.deleteDataById(id);
        return DataResult.success();
    }


    @PostMapping("/xpGzdd")
    @ApiOperation(value = "工作调度-下派工作调度")
    @LogAnnotation(title = "工作调度", action = "下派工作调度")
    @DS("oracle")
    public DataResult xpGzdd(@RequestBody JcAjxxEntity jcAjxxEntity) {
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkflowLogEntity::getId, jcAjxxEntity.getDduuid());
        WorkflowLogEntity workflowLogEntity = workflowLogService.getOne(queryWrapper);
        //根据案件状态自动分配下派人员 ajzt为标注,true代表正常下发，false代表未查询到执行人员
        boolean ajzt = true;
        switch (jcAjxxEntity.getAjjcztDm()) {
            case Constants.AJXX_ZT_ZZZX:
                workflowLogEntity.setTaxorgcode(jcAjxxEntity.getZxry());
                break;
            case Constants.AJXX_ZT_ZZSL:
                workflowLogEntity.setTaxorgcode(jcAjxxEntity.getSlry());
                break;
            case Constants.AJXX_ZT_ZZJC:
                workflowLogEntity.setTaxorgcode(jcAjxxEntity.getJcry());
                break;
            default:
                ajzt = false;
                break;
        }
        workflowLogService.updateById(workflowLogEntity);
        return DataResult.success(ajzt);
    }

    @PostMapping("/xpGzddry")
    @ApiOperation(value = "工作调度-下派工作调度人员")
    @LogAnnotation(title = "工作调度", action = "下派工作调度人员")
    @DS("oracle")
    public DataResult xpGzddry(@RequestBody JcAjxxEntity jcAjxxEntity) {
        LambdaQueryWrapper<GzddAjContentEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(GzddAjContentEntity::getDduuid, jcAjxxEntity.getDduuid());
        queryWrapper.eq(GzddAjContentEntity::getJcajxxuuid, jcAjxxEntity.getJcajxxuuid());
        GzddAjContentEntity gzddAjContentEntity = gzddAjContentService.getOne(queryWrapper);
        String ddjsry = TsBqUtils.isNull(jcAjxxEntity.getDdjsry()) ? jcAjxxEntity.getSelectedValue() : jcAjxxEntity.getDdjsry();
        gzddAjContentEntity.setDdjsry(ddjsry);
        gzddAjContentService.updateById(gzddAjContentEntity);
        LambdaQueryWrapper<WorkflowLogEntity> queryWorkWrapper = Wrappers.lambdaQuery();
        queryWorkWrapper.eq(WorkflowLogEntity::getId, jcAjxxEntity.getDduuid());
        WorkflowLogEntity workflowLogEntity = workflowLogService.getOne(queryWorkWrapper);
        workflowLogEntity.setTaxorgcode(jcAjxxEntity.getSelectedValue());
        workflowLogService.updateById(workflowLogEntity);
        return DataResult.success();
    }

    @PostMapping("/tbGzdd")
    @ApiOperation(value = "工作调度-填报工作调度")
    @LogAnnotation(title = "工作调度", action = "填报工作调度")
    public DataResult tbGzdd(@RequestBody AjGzddRequest ajGzddRequest) {
        gzddAjContentService.tbGzdd(ajGzddRequest);
        return DataResult.success();
    }

    @PostMapping("/stopGzdd/{id}")
    @ApiOperation(value = "暂停工作调度")
    @LogAnnotation(title = "暂停工作调度", action = "暂停")
    @DS("oracle")
    public DataResult stopGzdd(@PathVariable("id") String id) {
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkflowLogEntity::getId, id);
        WorkflowLogEntity workflowLogEntity = workflowLogService.getOne(queryWrapper);
        workflowLogEntity.setType(Constants.GZDD_DD_PAUSE);
        workflowLogService.updateById(workflowLogEntity);
        return DataResult.success();
    }

    @PostMapping("/exportAjlist/")
    @ApiOperation(value = "工作调度-导入案件列表")
    @LogAnnotation(title = "工作调度", action = "导入案件列表")
    public DataResult exportAjlist(@RequestParam(value = "file") MultipartFile file) throws IOException {
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        List<List<Object>> allAjbhList = reader.read(1, reader.getRowCount());
        return DataResult.success(allAjbhList);
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    @ApiOperation(value = "工作日志-个人列表")
    @LogAnnotation(title = "工作日志", action = "个人列表")
    @PostMapping("/list")
    @RequiresPermissions("workflow:log:list")
    @ResponseBody
    @DS("oracle")
    public DataResult list(@RequestBody WorkflowLogEntity workflowLog) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currentUserId = httpSessionService.getCurrentUserId();
        Page page = new Page(workflowLog.getPage(), workflowLog.getLimit());
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        if (!StringUtils.isEmpty(workflowLog.getStartTime())) {
            queryWrapper.gt(WorkflowLogEntity::getCreateTime, sdf.parse(workflowLog.getStartTime()));
        }
        if (!StringUtils.isEmpty(workflowLog.getEndTime())) {
            queryWrapper.lt(WorkflowLogEntity::getCreateTime, sdf.parse(workflowLog.getEndTime()));
        }
        String type = workflowLog.getType();
        if (!StringUtils.isEmpty(type)) {
            queryWrapper.eq(WorkflowLogEntity::getType, type);
        }
        // queryWrapper.eq(WorkflowLogEntity::getCreateId, currentUserId);
        queryWrapper.like(WorkflowLogEntity::getTaxPerson, currentUserId);
        queryWrapper.orderByDesc(WorkflowLogEntity::getCreateTime);
        IPage<WorkflowLogEntity> iPage = workflowLogService.page(page, queryWrapper);
        List<WorkflowLogEntity> records = iPage.getRecords();
        for (WorkflowLogEntity workflowLogEntity : records) {
            String type2 = workflowLogEntity.getType();
            if (type2.equals(WorkflowConstants.LOG_TYPE_2)) {
                // 处理案件日志，重新set CONTENT1
                String mold = workflowLogEntity.getMold();
                if (TsBqUtils.isNotNull(mold)) {
                    WorkflowLogSurveyEntity logSurvey = workflowLogSurveyService.getById(mold);
                    if (TsBqUtils.isNotNull(logSurvey)) {
                        mold = logSurvey.getTitle();
                    }
                }
                workflowLogEntity.setContent1(mold);
                String relevance = workflowLogEntity.getRelevance();
                JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
                if (TsBqUtils.isNotNull(jcAjxxEntity)) {
                    String nsrmc = jcAjxxEntity.getNsrmc();
                    String nsrsbh = jcAjxxEntity.getNsrsbh();
                    workflowLogEntity.setNsrmc(nsrmc);
                    workflowLogEntity.setNsrsbh(nsrsbh);
                }
            }
            // 处理 createFlag 字段
            if (workflowLogEntity.getTaxPerson().contains(workflowLogEntity.getCreateId())) {
                workflowLogEntity.setCreateFlag("Y");
            }
        }
        return DataResult.success(iPage);
    }


    @SuppressWarnings({"unchecked", "rawtypes"})
    @ApiOperation(value = "工作调度-查询调度列表")
    @LogAnnotation(title = "工作调度", action = "查询调度列表")
    @PostMapping("/queryGzddLists")
    @ResponseBody
    @DS("oracle")
    public DataResult queryGzddLists(@RequestBody WorkflowLogEntity workflowLog) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Page page = new Page(workflowLog.getPage(), workflowLog.getLimit());
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        if (!StringUtils.isEmpty(workflowLog.getStartTime())) {
            queryWrapper.gt(WorkflowLogEntity::getCreateTime, sdf.parse(workflowLog.getStartTime()));
        }
        if (!StringUtils.isEmpty(workflowLog.getEndTime())) {
            queryWrapper.lt(WorkflowLogEntity::getCreateTime, sdf.parse(workflowLog.getEndTime()));
        }
        if (!StringUtils.isEmpty(workflowLog.getDdmc())) {
            queryWrapper.like(WorkflowLogEntity::getDdmc, workflowLog.getDdmc());
        }
        if (TsBqUtils.isNotNull(workflowLog.getType())) {
            queryWrapper.eq(WorkflowLogEntity::getType, workflowLog.getType());
        } else {
            if (workflowLog.getBz() == Constants.LEVEL_QUANSHI) {
                queryWrapper.in(WorkflowLogEntity::getType, Constants.GZDD_DD_QS_ARRAY);
            } else {
                queryWrapper.in(WorkflowLogEntity::getType, Constants.GZDD_DD_QJ_ARRAY);
            }
        }
        queryWrapper.orderByDesc(WorkflowLogEntity::getCreateTime);
        IPage<WorkflowLogEntity> iPage = workflowLogService.page(page, queryWrapper);
        List<WorkflowLogEntity> records = iPage.getRecords();
        for (WorkflowLogEntity workflowLogEntity : records) {
            // 处理案件日志，重新set CONTENT1
            String mold = workflowLogEntity.getMold();
            if (TsBqUtils.isNotNull(mold)) {
                GzddmbEntity gzddmbEntity = gzddmbService.getById(mold);
                if (TsBqUtils.isNotNull(gzddmbEntity)) {
                    mold = gzddmbEntity.getTitle();
                }
            }
            workflowLogEntity.setContent1(mold);
            String id = workflowLogEntity.getId();
            LambdaQueryWrapper<GzddAjContentEntity> queryAjContentWrapper = Wrappers.lambdaQuery();
            queryAjContentWrapper.eq(GzddAjContentEntity::getDduuid, id);
            List<GzddAjContentEntity> gzddAjContentEntityList = gzddAjContentService.list(queryAjContentWrapper);
            long filledCount = gzddAjContentEntityList.stream()
                    .filter(entity -> "已填报".equals(entity.getDdzt()))
                    .count();
            long unfilledCount = gzddAjContentEntityList.stream()
                    .filter(entity -> {
                        String ddzt = entity.getDdzt();
                        return ddzt == null || ddzt.isEmpty() || "未填报".equals(ddzt);
                    })
                    .count();
            workflowLogEntity.setTbzt("已填报: " + filledCount + ", 未填报: " + unfilledCount);
        }

        return DataResult.success(iPage);
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    @ApiOperation(value = "工作调度-查询市局级调度关联案件表格")
    @LogAnnotation(title = "工作调度", action = "查询调度关联案件表格")
    @PostMapping("/queryGzddAjTables")
    @ResponseBody
    @DS("oracle")
    public DataResult queryGzddAjTables(@RequestBody WorkflowLogEntity workflowLog) {
        // 以工作调度-案件信息表为主
        GzddAjContentEntity gzddAjContentEntity = new GzddAjContentEntity();
        Page page = new Page(gzddAjContentEntity.getPage(), gzddAjContentEntity.getLimit());
        LambdaQueryWrapper<GzddAjContentEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(GzddAjContentEntity::getDduuid, workflowLog.getId());
        IPage<GzddAjContentEntity> iPage = this.gzddAjContentService.page(page, queryWrapper);
        List<GzddAjContentEntity> gzddAjContentEntityList = iPage.getRecords();
        for (GzddAjContentEntity ajContentEntity : gzddAjContentEntityList) {
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(ajContentEntity.getJcajxxuuid());
            String ajjczt = jcAjxxEntity.getAjjcztDm();
            DmJcAjztEntity dmJcAjzt = dmJcAjztService.getByAjztDm(ajjczt);
            if (TsBqUtils.isNotNull(dmJcAjzt)) {
                ajjczt = dmJcAjzt.getAjztmc();
            }
            ajContentEntity.setAjjcztDm(ajjczt);
            ajContentEntity.setAjContentId(ajContentEntity.getUuid());
            ajContentEntity.setAjcontent(ajContentEntity.getAjcontent());
            ajContentEntity.setJcajbh(jcAjxxEntity.getJcajbh());
            ajContentEntity.setAjmc(jcAjxxEntity.getAjmc());
            ajContentEntity.setAjzt(ajjczt);
            ajContentEntity.setNsrsbh(jcAjxxEntity.getNsrsbh());
            ajContentEntity.setNsrmc(jcAjxxEntity.getNsrmc());
        }
        return DataResult.success(iPage);
    }


    @SuppressWarnings({"unchecked", "rawtypes"})
    @ApiOperation(value = "工作调度-查询市局级调度关联案件表格")
    @LogAnnotation(title = "工作调度", action = "查询调度关联案件表格")
    @PostMapping("/queryGzddLogTables")
    @ResponseBody
    @DS("oracle")
    public DataResult queryGzddLogTables(@RequestBody WorkflowLogEntity workflowLog) {
        GzddAjContentEntity gzddAjContentEntity = new GzddAjContentEntity();
        Page page = new Page(gzddAjContentEntity.getPage(), gzddAjContentEntity.getLimit());
        //先根据jcajxxuuid 查询 工作调度表
        LambdaQueryWrapper<GzddAjContentEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(GzddAjContentEntity::getJcajxxuuid, workflowLog.getJcajxxuuids());
        queryWrapper.orderByDesc(GzddAjContentEntity::getCreateTime);
        IPage<GzddAjContentEntity> iPage = this.gzddAjContentService.page(page, queryWrapper);
        List<GzddAjContentEntity> records = iPage.getRecords();
        for (GzddAjContentEntity record : records) {
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(record.getJcajxxuuid());
            record.setJcajbh(jcAjxxEntity.getJcajbh());
            record.setAjmc(jcAjxxEntity.getAjmc());
            String ajztmc = dmJcAjztService.getAjztmcByajjcztDm(jcAjxxEntity.getAjjcztDm());
            if (TsBqUtils.isNotNull(ajztmc)) {
                record.setAjzt(ajztmc);
            }
            record.setNsrsbh(jcAjxxEntity.getNsrsbh());
            record.setNsrmc(jcAjxxEntity.getNsrmc());
            WorkflowLogEntity workflowLogEntity = workflowLogService.getById(record.getDduuid());
            record.setDdmc(workflowLogEntity.getDdmc());
            record.setMold(workflowLogEntity.getMold());
            String mbmc = gzddmbService.getById(workflowLogEntity.getMold()).getTitle();
            record.setMbmc(mbmc);
            if (TsBqUtils.isNull(record.getDdjsry())) {
                record.setDdjsry("暂未委派人员");
            }
        }
        return DataResult.success(iPage);
    }

    List<String> queryJcajxxuuidList(@RequestBody WorkflowLogEntity workflowLog) {
        LambdaQueryWrapper<GzddAjContentEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(GzddAjContentEntity::getDduuid, workflowLog.getId());
        List<GzddAjContentEntity> gzddAjContentEntityList = gzddAjContentService.list(queryWrapper);
        return gzddAjContentEntityList.stream().map(GzddAjContentEntity::getJcajxxuuid).collect(Collectors.toList()); // 收集到 List 中;
    }

    @ApiOperation(value = "工作调度-查询个人调度关联案件表格")
    @LogAnnotation(title = "工作调度", action = "查询调度关联案件表格")
    @PostMapping("/queryGzddGrAjTables")
    @ResponseBody
    @DS("oracle")
    public DataResult queryGzddGrAjTables(@RequestBody WorkflowLogEntity workflowLog) {
        String currentUserIdId = httpSessionService.getCurrentUserId();
        GzddAjContentEntity gzddAjContentEntity = new GzddAjContentEntity();
        Page<GzddAjContentEntity> page = new Page<>(gzddAjContentEntity.getPage(), gzddAjContentEntity.getLimit());
        LambdaQueryWrapper<GzddAjContentEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(GzddAjContentEntity::getDduuid, workflowLog.getId())
                .like(GzddAjContentEntity::getDdjsry, currentUserIdId)
                .orderByDesc(GzddAjContentEntity::getCreateTime);
        page = this.gzddAjContentService.page(page, queryWrapper);
        List<GzddAjContentEntity> gzddAjContentEntityList = page.getRecords();
        // 然后再做数据增强（设置案卷状态等）
        for (GzddAjContentEntity ajContentEntity : gzddAjContentEntityList) {
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getById(ajContentEntity.getJcajxxuuid());
            String ajjczt = jcAjxxEntity.getAjjcztDm();
            DmJcAjztEntity dmJcAjzt = dmJcAjztService.getByAjztDm(ajjczt);
            if (TsBqUtils.isNotNull(dmJcAjzt)) {
                ajjczt = dmJcAjzt.getAjztmc();
            }
            ajContentEntity.setAjzt(ajjczt);
            ajContentEntity.setAjContentId(ajContentEntity.getUuid());
            ajContentEntity.setNsrsbh(jcAjxxEntity.getNsrsbh());
            ajContentEntity.setNsrmc(jcAjxxEntity.getNsrmc());
            ajContentEntity.setJcajbh(jcAjxxEntity.getJcajbh());
            ajContentEntity.setAjmc(jcAjxxEntity.getAjmc());
        }
        return DataResult.success(page);
    }

    @ApiOperation(value = "工作调度-查询调度列表")
    @LogAnnotation(title = "工作调度", action = "查询调度列表")
    @PostMapping("/exportGzddLists")
    @ResponseBody
    @DS("oracle")
    public DataResult exportGzddLists(@RequestBody WorkflowLogEntity workflowLog) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        if (!StringUtils.isEmpty(workflowLog.getStartTime())) {
            queryWrapper.gt(WorkflowLogEntity::getCreateTime, sdf.parse(workflowLog.getStartTime()));
        }
        if (!StringUtils.isEmpty(workflowLog.getEndTime())) {
            queryWrapper.lt(WorkflowLogEntity::getCreateTime, sdf.parse(workflowLog.getEndTime()));
        }
        queryWrapper.eq(WorkflowLogEntity::getType, Constants.GZDD_DD_FINISH);
        queryWrapper.orderByDesc(WorkflowLogEntity::getCreateTime);
        List<WorkflowLogEntity> workflowLogEntityList = workflowLogService.list(queryWrapper);
        for (WorkflowLogEntity workflowLogEntity : workflowLogEntityList) {
            // 处理案件日志，重新set CONTENT1
            String mold = workflowLogEntity.getMold();
            if (TsBqUtils.isNotNull(mold)) {
                GzddmbEntity gzddmbEntity = gzddmbService.getById(mold);
                if (TsBqUtils.isNotNull(gzddmbEntity)) {
                    mold = gzddmbEntity.getTitle();
                }
                workflowLogEntity.setType("已完成调度");
            }
            workflowLogEntity.setContent1(mold);
            if (TsBqUtils.isNotNull(workflowLogEntity.getTaxorgcode())) {
                workflowLogEntity.setTaxorgcode(dmGySwjgService.getSwjgDm(workflowLogEntity.getTaxorgcode()).getSwjgjc());
            }
        }
        return DataResult.success(workflowLogEntityList);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "工作日志-科长审批列表")
    @LogAnnotation(title = "工作日志", action = "科长审批列表")
    @PostMapping("/listbydiscipline")
    @RequiresPermissions("workflow:log:listbydiscipline")
    @ResponseBody
    @DS("oracle")
    public DataResult listbydiscipline(@RequestBody WorkflowLogEntity workflowLog) {
        String currentDeptId = httpSessionService.getCurrentDeptId();
        Page page = new Page(workflowLog.getPage(), workflowLog.getLimit());
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.eq(WorkflowLogEntity::getTaxorgcode, currentDeptId);
        queryWrapper.isNull(WorkflowLogEntity::getDiscipline);
        IPage<WorkflowLogEntity> iPage = workflowLogService.page(page, queryWrapper);
        List<WorkflowLogEntity> records = iPage.getRecords();
        for (WorkflowLogEntity workflowLogEntity : records) {
            String type = workflowLogEntity.getType();
            if (type.equals(WorkflowConstants.LOG_TYPE_2)) {
                String relevance = workflowLogEntity.getRelevance();
                JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
                if (TsBqUtils.isNotNull(jcAjxxEntity)) {
                    String nsrmc = jcAjxxEntity.getNsrmc();
                    String nsrsbh = jcAjxxEntity.getNsrsbh();
                    workflowLogEntity.setNsrmc(nsrmc);
                    workflowLogEntity.setNsrsbh(nsrsbh);
                }
            }

            String createId = workflowLogEntity.getCreateId();
            SysUser sysUser = userService.getUser(createId);
            String realName = sysUser.getRealName();
            if (TsBqUtils.isNotNull(realName)) {
                workflowLogEntity.setCreateId(realName);
            }
            if (type.equals(WorkflowConstants.LOG_TYPE_2)) {
                // 处理案件日志，重新set CONTENT1
                String mold = workflowLogEntity.getMold();
                if (TsBqUtils.isNotNull(mold)) {
                    WorkflowLogSurveyEntity logSurvey = workflowLogSurveyService.getById(mold);
                    if (TsBqUtils.isNotNull(logSurvey)) {
                        mold = logSurvey.getTitle();
                    }
                }
                workflowLogEntity.setContent1(mold);
            }
        }
        return DataResult.success(iPage);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "工作日志-科长审批列表(已审)")
    @LogAnnotation(title = "工作日志", action = "科长审批列表(已审)")
    @PostMapping("/listbydisciplinedeno")
    @RequiresPermissions("workflow:log:listbydisciplinedeno")
    @ResponseBody
    @DS("oracle")
    public DataResult listbydisciplinedeno(@RequestBody WorkflowLogEntity workflowLog) {

        String currentUserId = httpSessionService.getCurrentUserId();

        Page page = new Page(workflowLog.getPage(), workflowLog.getLimit());
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.eq(WorkflowLogEntity::getDiscipline, currentUserId);

        IPage<WorkflowLogEntity> iPage = workflowLogService.page(page, queryWrapper);

        List<WorkflowLogEntity> records = iPage.getRecords();
        for (WorkflowLogEntity workflowLogEntity : records) {
            String createId = workflowLogEntity.getCreateId();
            SysUser sysUser = userService.getUser(createId);
            String realName = sysUser.getRealName();
            if (TsBqUtils.isNotNull(realName)) {
                workflowLogEntity.setCreateId(realName);
            }
            String type = workflowLogEntity.getType();
            if (type.equals(WorkflowConstants.LOG_TYPE_2)) {
                String relevance = workflowLogEntity.getRelevance();
                JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
                if (TsBqUtils.isNotNull(jcAjxxEntity)) {
                    String nsrmc = jcAjxxEntity.getNsrmc();
                    String nsrsbh = jcAjxxEntity.getNsrsbh();
                    workflowLogEntity.setNsrmc(nsrmc);
                    workflowLogEntity.setNsrsbh(nsrsbh);

                }
            }
            if (type.equals(WorkflowConstants.LOG_TYPE_2)) {
                // 处理案件日志，重新set CONTENT1
                String mold = workflowLogEntity.getMold();
                if (TsBqUtils.isNotNull(mold)) {
                    WorkflowLogSurveyEntity logSurvey = workflowLogSurveyService.getById(mold);
                    if (TsBqUtils.isNotNull(logSurvey)) {
                        mold = logSurvey.getTitle();
                    }
                }
                workflowLogEntity.setContent1(mold);
            }
        }
        return DataResult.success(iPage);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "工作日志-纪检审批列表")
    @LogAnnotation(title = "工作日志", action = "纪检审批列表")
    @PostMapping("/listbysectionchief")
    @RequiresPermissions("workflow:log:listbysectionchief")
    @ResponseBody
    @DS("oracle")
    public DataResult listbysectionchief(@RequestBody WorkflowLogEntity workflowLog) {
        String currentDeptId = httpSessionService.getCurrentDeptId();
        Page page = new Page(workflowLog.getPage(), workflowLog.getLimit());
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.eq(WorkflowLogEntity::getTaxorgcode, currentDeptId);
        queryWrapper.isNull(WorkflowLogEntity::getSectionchief);
        IPage<WorkflowLogEntity> iPage = workflowLogService.page(page, queryWrapper);
        List<WorkflowLogEntity> records = iPage.getRecords();
        for (WorkflowLogEntity workflowLogEntity : records) {
            String type = workflowLogEntity.getType();
            if (type.equals(WorkflowConstants.LOG_TYPE_2)) {
                String relevance = workflowLogEntity.getRelevance();
                JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
                if (TsBqUtils.isNotNull(jcAjxxEntity)) {
                    String nsrmc = jcAjxxEntity.getNsrmc();
                    String nsrsbh = jcAjxxEntity.getNsrsbh();
                    workflowLogEntity.setNsrmc(nsrmc);
                    workflowLogEntity.setNsrsbh(nsrsbh);
                }
            }

            String createId = workflowLogEntity.getCreateId();
            SysUser sysUser = userService.getUser(createId);
            String realName = sysUser.getRealName();
            if (TsBqUtils.isNotNull(realName)) {
                workflowLogEntity.setCreateId(realName);
            }
            if (type.equals(WorkflowConstants.LOG_TYPE_2)) {
                // 处理案件日志，重新set CONTENT1
                String mold = workflowLogEntity.getMold();
                if (TsBqUtils.isNotNull(mold)) {
                    WorkflowLogSurveyEntity logSurvey = workflowLogSurveyService.getById(mold);
                    if (TsBqUtils.isNotNull(logSurvey)) {
                        mold = logSurvey.getTitle();
                    }
                }
                workflowLogEntity.setContent1(mold);
            }
        }
        return DataResult.success(iPage);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "工作调度-查询代办调度列表")
    @LogAnnotation(title = "工作调度", action = "查询代办调度列表")
    @PostMapping("/queryGzddContent")
    @ResponseBody
    @DS("oracle")
    public DataResult queryGzddContent(@RequestBody WorkflowLogEntity workflowLog) {
        String currentUserId = httpSessionService.getCurrentUserId();
        LambdaQueryWrapper<GzddAjContentEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询案件调度表
        queryWrapper.like(GzddAjContentEntity::getDdjsry, currentUserId);
        queryWrapper.orderByDesc(GzddAjContentEntity::getCreateTime);
        List<GzddAjContentEntity> gzddAjContentEntityList = gzddAjContentService.list(queryWrapper);
        List<String> idList = gzddAjContentEntityList.stream()
                .map(GzddAjContentEntity::getDduuid)
                .distinct() // 过滤掉重复的值
                .collect(Collectors.toList());
        if (TsBqUtils.isNotNull(idList)) {
            Map<String, List<GzddAjContentEntity>> groupedByDduuid = gzddAjContentEntityList.stream()
                    .collect(Collectors.groupingBy(GzddAjContentEntity::getDduuid));
            for (Map.Entry<String, List<GzddAjContentEntity>> entry : groupedByDduuid.entrySet()) {
                String dduuid = entry.getKey();
                List<GzddAjContentEntity> entities = entry.getValue();
                long filledCount = entities.stream()
                        .filter(entity -> "已填报".equals(entity.getDdzt()))
                        .count();
                long unfilledCount = entities.stream()
                        .filter(entity -> "未填报".equals(entity.getDdzt()))
                        .count();
                WorkflowLogEntity workflowLogEntity = new WorkflowLogEntity();
                workflowLogEntity.setId(dduuid);
                workflowLogEntity.setTbzt("已填报: " + filledCount + ",未填报: " + unfilledCount);
                workflowLogService.updateById(workflowLogEntity);
            }
            IPage page = new Page(workflowLog.getPage(), workflowLog.getLimit());
            LambdaQueryWrapper<WorkflowLogEntity> queryWorkWrapper = Wrappers.lambdaQuery();
            queryWorkWrapper.eq(WorkflowLogEntity::getType, Constants.GZDD_DD_PUSH).or().eq(WorkflowLogEntity::getType, Constants.GZDD_DD_FINISH);
            queryWorkWrapper.in(WorkflowLogEntity::getId, idList);
            //查询已推送，未办理的数据
            IPage<WorkflowLogEntity> iPage = workflowLogService.page(page, queryWorkWrapper);
            List<WorkflowLogEntity> records = iPage.getRecords();
            for (WorkflowLogEntity workflowLogEntity : records) {
                // 处理案件日志，重新set CONTENT1
                String mold = workflowLogEntity.getMold();
                if (TsBqUtils.isNotNull(mold)) {
                    GzddmbEntity gzddmbEntity = gzddmbService.getById(mold);
                    if (TsBqUtils.isNotNull(gzddmbEntity)) {
                        mold = gzddmbEntity.getTitle();
                    }
                }
                workflowLogEntity.setContent1(mold);
            }
            return DataResult.success(iPage);
        }
        return DataResult.success();
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "工作日志-纪检审批列表(已审)")
    @LogAnnotation(title = "工作日志", action = "纪检审批列表(已审)")
    @PostMapping("/listbysectionchiefdeno")
    @RequiresPermissions("workflow:log:listbysectionchiefdeno")
    @ResponseBody
    @DS("oracle")
    public DataResult listbysectionchiefdeno(@RequestBody WorkflowLogEntity workflowLog) {
        String currentUserId = httpSessionService.getCurrentUserId();
        Page page = new Page(workflowLog.getPage(), workflowLog.getLimit());
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.eq(WorkflowLogEntity::getSectionchief, currentUserId);
        IPage<WorkflowLogEntity> iPage = workflowLogService.page(page, queryWrapper);
        List<WorkflowLogEntity> records = iPage.getRecords();
        for (WorkflowLogEntity workflowLogEntity : records) {
            String createId = workflowLogEntity.getCreateId();
            SysUser sysUser = userService.getUser(createId);
            String realName = sysUser.getRealName();
            if (TsBqUtils.isNotNull(realName)) {
                workflowLogEntity.setCreateId(realName);
            }
            String type = workflowLogEntity.getType();
            if (type.equals(WorkflowConstants.LOG_TYPE_2)) {
                String relevance = workflowLogEntity.getRelevance();
                JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
                if (TsBqUtils.isNotNull(jcAjxxEntity)) {
                    String nsrmc = jcAjxxEntity.getNsrmc();
                    String nsrsbh = jcAjxxEntity.getNsrsbh();
                    workflowLogEntity.setNsrmc(nsrmc);
                    workflowLogEntity.setNsrsbh(nsrsbh);
                }
            }

            if (type.equals(WorkflowConstants.LOG_TYPE_2)) {
                // 处理案件日志，重新set CONTENT1
                String mold = workflowLogEntity.getMold();
                WorkflowLogSurveyEntity logSurvey = workflowLogSurveyService.getById(mold);
                if (TsBqUtils.isNotNull(logSurvey)) {
                    mold = logSurvey.getTitle();
                }
                workflowLogEntity.setContent1(mold);
            }
        }
        return DataResult.success(iPage);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "工作调度-查询调度内容完成列表")
    @LogAnnotation(title = "工作调度", action = "查询调度内容完成列表")
    @PostMapping("/queryFinishGzdd")
    @ResponseBody
    @DS("oracle")
    public DataResult queryFinishGzdd(@RequestBody WorkflowLogEntity workflowLog) {
        String currentUserId = httpSessionService.getCurrentUserId();
        Page page = new Page(workflowLog.getPage(), workflowLog.getLimit());
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.eq(WorkflowLogEntity::getSectionchief, currentUserId);
        queryWrapper.eq(WorkflowLogEntity::getType, Constants.GZDD_DD_FINISH);
        queryWrapper.orderByDesc(WorkflowLogEntity::getCreateTime);
        IPage<WorkflowLogEntity> iPage = workflowLogService.page(page, queryWrapper);
        List<WorkflowLogEntity> records = iPage.getRecords();
        for (WorkflowLogEntity workflowLogEntity : records) {
            String createId = workflowLogEntity.getCreateId();
            SysUser sysUser = userService.getUser(createId);
            String realName = sysUser.getRealName();
            if (TsBqUtils.isNotNull(realName)) {
                workflowLogEntity.setCreateId(realName);
            }
            String relevance = workflowLogEntity.getRelevance();
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            if (TsBqUtils.isNotNull(jcAjxxEntity)) {
                String nsrmc = jcAjxxEntity.getNsrmc();
                String nsrsbh = jcAjxxEntity.getNsrsbh();
                workflowLogEntity.setNsrmc(nsrmc);
                workflowLogEntity.setNsrsbh(nsrsbh);
            }
            // 处理案件日志，重新set CONTENT1
            String mold = workflowLogEntity.getMold();
            WorkflowLogSurveyEntity logSurvey = workflowLogSurveyService.getById(mold);
            if (TsBqUtils.isNotNull(logSurvey)) {
                mold = logSurvey.getTitle();
            }
            workflowLogEntity.setContent1(mold);
        }
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "工作日志-添加日常日志")
    @LogAnnotation(title = "工作日志", action = "添加日常日志")
    @PostMapping("/add")
    @RequiresPermissions("workflow:log:add")
    @ResponseBody
    @DS("oracle")
    public DataResult add(@RequestBody WorkflowLogEntity workflowLog) {
        Date date = new Date();
        String currentDeptId = httpSessionService.getCurrentDeptId();
        String currentUserId = httpSessionService.getCurrentUserId();
        String content1 = workflowLog.getContent1();
        String content2 = workflowLog.getContent2();
        String content3 = workflowLog.getContent3();
        String mold = workflowLog.getMold();
        WorkflowLogEntity workflowLogEntity = new WorkflowLogEntity();
        workflowLogEntity.setId(TsBqUtils.getUUID());
        workflowLogEntity.setType(WorkflowConstants.LOG_TYPE_1);
        workflowLogEntity.setCate(WorkflowConstants.LOG_CATE_2);
        workflowLogEntity.setDeleted(Constants.DELETEED_NOT);
        workflowLogEntity.setTaxorgcode(currentDeptId);
        workflowLogEntity.setCreateId(currentUserId);
        workflowLogEntity.setCreateTime(date);
        workflowLogEntity.setContent1(content1);
        workflowLogEntity.setContent2(content2);
        workflowLogEntity.setContent3(content3);
        workflowLogEntity.setMold(mold);
        workflowLogEntity.setContentdate(workflowLog.getContentdate());
        workflowLogEntity.setTaxPerson(currentUserId);
        //查询系统userID
        workflowLogEntity.setCreateName(userService.queryCreateName(currentUserId));
        workflowLogService.save(workflowLogEntity);
        return DataResult.success();
    }

    @ApiOperation(value = "工作调度-推送日常日志")
    @LogAnnotation(title = "工作日志", action = "添加日常日志")
    @PostMapping("/pushGzdd")
    @ResponseBody
    @DS("oracle")
    public DataResult pushGzdd(@RequestBody GzddworkEntity gzddworkEntity) {
        String currentUserId = httpSessionService.getCurrentUserId();
        String[] parts = gzddworkEntity.getSwjgDm().split("\\|");
        String swjgDm = parts[0]; // 获取 "12101009150"
        String swjgjc = parts[1]; // 获取 "沈阳市税务局第一稽查局案件审理科"
        //查询系统userID
        gzddworkEntity.setUuid(TsBqUtils.getUUID());
        gzddworkEntity.setMbid(gzddworkEntity.getMbid());
        gzddworkEntity.setAjbh(gzddworkEntity.getAjbh());
        gzddworkEntity.setDdzt("0");
        gzddworkEntity.setDdlx("Y");
        gzddworkEntity.setYxbz("Y");
        gzddworkEntity.setSwjgDm(swjgDm);
        gzddworkEntity.setSwjgmc(swjgjc);
        gzddworkEntity.setDdnr(gzddworkEntity.getDdnr());
        gzddworkEntity.setDdnrvalue(gzddworkEntity.getDdnrvalue());
        gzddworkEntity.setCreateName(userService.queryCreateName(currentUserId));
        gzddworkService.save(gzddworkEntity);
        return DataResult.success();
    }

    @ApiOperation(value = "工作日志-添加案卷日志")
    @LogAnnotation(title = "工作日志", action = "添加案卷日志")
    @PostMapping("/addinspectionhear")
    @RequiresPermissions("workflow:log:addinspectionhear")
    @ResponseBody
    @DS("oracle")
    public DataResult addinspectionhear(@RequestBody WorkflowLogEntity workflowLog) {
        Date date = new Date();
        String currentDeptId = httpSessionService.getCurrentDeptId();
        String currentUserId = httpSessionService.getCurrentUserId();
        String mold = workflowLog.getMold();
        Date contentdate = workflowLog.getContentdate();
        String relevance = workflowLog.getRelevance();
        WorkflowLogEntity workflowLogEntity = new WorkflowLogEntity();
        String workflowLogId = TsBqUtils.getUUID();
        workflowLogEntity.setId(workflowLogId);
        String type = WorkflowConstants.LOG_TYPE_2;
        workflowLogEntity.setType(type);
        String cate = WorkflowConstants.LOG_CATE_1 + "," + WorkflowConstants.LOG_CATE_2 + "," + WorkflowConstants.LOG_CATE_3;
        workflowLogEntity.setCate(cate);
        workflowLogEntity.setRelevance(relevance);
        workflowLogEntity.setDeleted(Constants.DELETEED_NOT);
        workflowLogEntity.setTaxorgcode(currentDeptId);
        workflowLogEntity.setCreateId(currentUserId);
        workflowLogEntity.setCreateTime(date);
        workflowLogEntity.setContentdate(contentdate);
        workflowLogEntity.setMold(mold);
        //解决 - sysuser表查询不到的问题
        String createName = userService.queryCreateName(currentUserId);
        workflowLogEntity.setCreateName(createName);
        // 写入分表
        String feelBack = workflowLog.getFeelBack();
        JSONArray feelBackJsonArray = JSON.parseArray(feelBack);
        for (Object o : feelBackJsonArray) {
            JSONObject optionsJsonObject = JSONObject.parseObject(o.toString());
            // String surveyId = optionsJsonObject.get("surveyId").toString();
            String optionsId = optionsJsonObject.get("optionsId").toString();
            String optionsType = optionsJsonObject.get("optionsType").toString();
            String optionsTitle = optionsJsonObject.get("optionsTitle").toString();
            String _feelBack = optionsJsonObject.get("feelBack").toString();
            // JSONArray parseArray = JSON.parseArray(_feelBack);
//            if (optionsType.equals("1")) {
//                // 文本逻辑
//            } else if (optionsType.equals("2")) {
//
//            } else if (optionsType.equals("3")) {
//
//            }
            // 保存服务端
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            /*
             * "insert into TS_SQ_BS_SURVEY_FEEDBACK (ID,SURVEY_ID,BATCH_ID,SURVEY_MESSAGE_ID,OPTIONS_ID,OPTIONS_TITLE,OPTIONS_CONENT,OPTIONS_FEEDBACK,OPTIONS_TYPE,OPTIONS_RELATION,OPTIONS_NO,DJXH,FIELD4,FIELD5,FIELD6,FIELD7,FIELD8,FIELD9,TAXORGCODE,DELETED,CREATE_ID,UPDATE_ID,CREATE_TIME,UPDATE_TIME)"
             * "values ('"+uuid+"','"+surveyId+"','','"+surveyMessageId+"','"+
             * optionsId+"','"+optionsTitle+"','','"+_feelBack+"','"+optionsType
             * +"','','','"+djxh+"','','','','','','','"+taxorgcode+
             * "','1','','',sysdate,'')";
             */
            WorkflowLogSurveyFeedbackEntity surveyFeedback = new WorkflowLogSurveyFeedbackEntity();
            surveyFeedback.setId(uuid);
            surveyFeedback.setSurveyId(mold);
            surveyFeedback.setBatchId(workflowLogId); // 关联日志UUID
            surveyFeedback.setSurveyMessageId(relevance);
            surveyFeedback.setOptionsId(optionsId);
            surveyFeedback.setOptionsTitle(optionsTitle);
            surveyFeedback.setOptionsConent("");
            surveyFeedback.setOptionsFeedback(_feelBack);
            surveyFeedback.setOptionsType(optionsType);
            surveyFeedback.setOptionsRelation("");
            surveyFeedback.setTaxorgcode(currentDeptId);
            surveyFeedback.setDeleted(Constants.DATA_NOT_DELETED);
            surveyFeedback.setCreateId(currentUserId);
            // surveyFeedback.setUpdateId();
            surveyFeedback.setCreateTime(date);
            // surveyFeedback.setUpdateTime();
            workflowLogSurveyFeedbackService.save(surveyFeedback);
        }

        // 写入确认表 TODO
        String currentRolesId = httpSessionService.getCurrentRolesId();
        boolean containsJcry = currentRolesId.contains(WorkflowConstants.ROLE_1708113478976823298); // 检查-检查人岗位
        boolean containsZxry = currentRolesId.contains(WorkflowConstants.ROLE_1750438346513281026); // 执行-执行员岗位
        JcAjxxEntity jcAjxx = jcAjxxService.getByJcajxxuuid(relevance);
        // 写入主表
        // 检查人
        if (containsJcry) {
            String jcry = jcAjxx.getJcry();
            workflowLogEntity.setTaxPerson(jcry);
            List<String> list = Arrays.stream(jcry.split(",")).collect(Collectors.toList());
            for (String _jcry : list) {
                if (!_jcry.contains(currentUserId)) {
                    WorkflowLogConfirmEntity workflowLogConfirmEntity = new WorkflowLogConfirmEntity();
                    workflowLogConfirmEntity.setId(TsBqUtils.getUUID());
                    workflowLogConfirmEntity.setRelevance(relevance);
                    workflowLogConfirmEntity.setCate("检查日志");
                    workflowLogConfirmEntity.setTaxpeople(_jcry);
                    workflowLogConfirmEntity.setLogId(workflowLogId);
                    workflowLogConfirmEntity.setMold(mold);
                    workflowLogConfirmEntity.setContentdate(contentdate);
                    workflowLogConfirmEntity.setCate(cate);
                    workflowLogConfirmEntity.setType(type);
                    workflowLogConfirmEntity.setTaxorgcode(currentDeptId);
                    workflowLogConfirmEntity.setCreateId(currentUserId);
                    workflowLogConfirmEntity.setCreateTime(date);
                    workflowLogConfirmEntity.setDeleted(Constants.DELETEED_NOT);
                    workflowLogConfirmEntity.setCreateName(createName);
                    workflowLogConfirmService.save(workflowLogConfirmEntity);
                }
            }
        }
        // 执行人
        if (containsZxry) {
            String zxry = jcAjxx.getZxry();
            workflowLogEntity.setTaxPerson(zxry);
            List<String> list = Arrays.stream(zxry.split(",")).collect(Collectors.toList());
            for (String _zxry : list) {
                if (!_zxry.contains(currentUserId)) {
                    WorkflowLogConfirmEntity workflowLogConfirmEntity = new WorkflowLogConfirmEntity();
                    workflowLogConfirmEntity.setId(TsBqUtils.getUUID());
                    workflowLogConfirmEntity.setRelevance(relevance);
                    workflowLogConfirmEntity.setCate("执行日志");
                    workflowLogConfirmEntity.setTaxpeople(_zxry);
                    workflowLogConfirmEntity.setLogId(workflowLogId);
                    workflowLogConfirmEntity.setMold(mold);
                    workflowLogConfirmEntity.setContentdate(contentdate);
                    workflowLogConfirmEntity.setCate(cate);
                    workflowLogConfirmEntity.setType(type);
                    workflowLogConfirmEntity.setTaxorgcode(currentDeptId);
                    workflowLogConfirmEntity.setCreateId(currentUserId);
                    workflowLogConfirmEntity.setCreateTime(date);
                    workflowLogConfirmEntity.setDeleted(Constants.DELETEED_NOT);
                    workflowLogConfirmEntity.setCreateName(createName);
                    workflowLogConfirmService.save(workflowLogConfirmEntity);
                }
            }
        }
        workflowLogService.save(workflowLogEntity);
        return DataResult.success();
    }

    @ApiOperation(value = "工作调度-添加工作调度")
    @LogAnnotation(title = "工作调度", action = "添加工作调度")
    @PostMapping("/addGzdd")
    @ResponseBody
    @DS("oracle")
    public DataResult addGzdd(@RequestBody AjGzddRequest ajGzddRequest) {
        Date date = new Date();
        String currentUserId = httpSessionService.getCurrentUserId();
        //模板id
        String mold = ajGzddRequest.getMold();
        //调度时间
        Date contentdate = ajGzddRequest.getContentdate();
        WorkflowLogEntity workflowLogEntity = new WorkflowLogEntity();
        //如果为空就保存，不为空就更新
        if (TsBqUtils.isNull(ajGzddRequest.getId())) {
            String workflowLogId = TsBqUtils.getUUID();
            workflowLogEntity.setId(workflowLogId);
            //调度类型.默认为保存 0 为 市级保存 1 为 局级保存
            workflowLogEntity.setType(ajGzddRequest.getBz() == 0 ? Constants.GZDD_DD_S_SAVE : Constants.GZDD_DD_J_SAVE);
            // 模板类型
            String cate = WorkflowConstants.LOG_CATE_1 + "," + WorkflowConstants.LOG_CATE_2 + "," + WorkflowConstants.LOG_CATE_3;
            workflowLogEntity.setCate(cate);
            workflowLogEntity.setDeleted(Constants.DELETEED_NOT);
            //创建人
            workflowLogEntity.setCreateId(currentUserId);
            //创建时间
            workflowLogEntity.setCreateTime(date);
            //创建时间
            workflowLogEntity.setContentdate(contentdate);
            //模板ID
            workflowLogEntity.setMold(mold);
            //创建人
            workflowLogEntity.setTaxPerson(currentUserId);
            //调度名称
            workflowLogEntity.setDdmc(ajGzddRequest.getDdmc());
            //稽查案件信息UUID合集
            String jcajxxuuids = ajGzddRequest.getJcajxxuuids().substring(1, ajGzddRequest.getJcajxxuuids().length() - 1);
            workflowLogEntity.setJcajxxuuids(jcajxxuuids);
            //创建人名称
            workflowLogEntity.setCreateName(currentUserId + "|" + dmGySwryService.getSwry(currentUserId).getSwryxm());
            //保存工作调度-案件列表内容信息
            saveGzddAjContent(ajGzddRequest, workflowLogId);
            // 写入确认表
            workflowLogService.save(workflowLogEntity);
        } else {
            workflowLogEntity.setId(ajGzddRequest.getId());
            //调度类型.默认为保存 0 为 市级保存 1 为 局级保存
            String type = judgeType(ajGzddRequest);
            workflowLogEntity.setType(type);
            // 模板类型
            String cate = WorkflowConstants.LOG_CATE_1 + "," + WorkflowConstants.LOG_CATE_2 + "," + WorkflowConstants.LOG_CATE_3;
            workflowLogEntity.setCate(cate);
            workflowLogEntity.setDeleted(Constants.DELETEED_NOT);
            //创建人
            workflowLogEntity.setCreateId(currentUserId);
            //创建时间
            workflowLogEntity.setCreateTime(date);
            //完成时间
            workflowLogEntity.setUpdateTime(Constants.GZDD_DD_FINISH.equals(type) ? date : null);
            //创建时间
            workflowLogEntity.setContentdate(contentdate);
            //模板ID
            workflowLogEntity.setMold(mold);
            //创建人
            workflowLogEntity.setTaxPerson(currentUserId);
            //调度名称
            workflowLogEntity.setDdmc(ajGzddRequest.getDdmc());
            //稽查案件信息UUID合集
            String jcajxxuuids = ajGzddRequest.getJcajxxuuids().substring(1, ajGzddRequest.getJcajxxuuids().length() - 1);
            workflowLogEntity.setJcajxxuuids(jcajxxuuids);
            //创建人名称
            workflowLogEntity.setCreateName(currentUserId + "|" + dmGySwryService.getSwry(currentUserId).getSwryxm());
            //保存工作调度-案件列表内容信息
            saveGzddAjContent(ajGzddRequest, ajGzddRequest.getId());
            // 写入确认表
            workflowLogService.updateById(workflowLogEntity);
        }
        return DataResult.success();
    }

    //保存工作调度-案件列表内容信息
    public void saveGzddAjContent(AjGzddRequest ajGzddRequest, String workflowLogId) {
        //保存案件调度-案件内容信息
        for (int i = 0; i < ajGzddRequest.getAjList().size(); i++) {
            GzddAjContentEntity gzddAjContentEntity = new GzddAjContentEntity();
            if (TsBqUtils.isNull(ajGzddRequest.getAjList().get(i).getAjContentId())) {
                gzddAjContentEntity.setUuid(TsBqUtils.getUUID());
                gzddAjContentEntity.setDduuid(workflowLogId);
                gzddAjContentEntity.setJcajxxuuid(ajGzddRequest.getAjList().get(i).getJcajxxuuid());
                gzddAjContentEntity.setCreateTime(TsBqUtils.getSystemTime());
                gzddAjContentEntity.setDdjsry(ajGzddRequest.getAjList().get(i).getDdjsry());
                gzddAjContentEntity.setAjcontent(ajGzddRequest.getAjList().get(i).getAjContent());
                gzddAjContentEntity.setDdzt(TsBqUtils.isNotNull(ajGzddRequest.getAjList().get(i).getAjContent()) ? "已填报" : "未填报");
                gzddAjContentEntity.setBz(ajGzddRequest.getBz());
                gzddAjContentService.save(gzddAjContentEntity);
            } else {
                gzddAjContentEntity.setUuid(ajGzddRequest.getAjList().get(i).getAjContentId());
                gzddAjContentEntity.setDduuid(workflowLogId);
                gzddAjContentEntity.setJcajxxuuid(ajGzddRequest.getAjList().get(i).getJcajxxuuid());
                gzddAjContentEntity.setCreateTime(TsBqUtils.getSystemTime());
                gzddAjContentEntity.setDdjsry(ajGzddRequest.getAjList().get(i).getDdjsry());
                gzddAjContentEntity.setAjcontent(ajGzddRequest.getAjList().get(i).getAjContent());
                gzddAjContentEntity.setDdzt(TsBqUtils.isNotNull(ajGzddRequest.getAjList().get(i).getAjContent()) ? "已填报" : "未填报");
                gzddAjContentEntity.setBz(ajGzddRequest.getBz());
                gzddAjContentService.updateById(gzddAjContentEntity);
            }
        }
    }

    public String judgeType(AjGzddRequest ajGzddRequest) {
        String type = ajGzddRequest.getType();
        //判断当前调度是否完成
        LambdaQueryWrapper<GzddAjContentEntity> queryGzddContentWrapper = Wrappers.lambdaQuery();
        queryGzddContentWrapper.eq(GzddAjContentEntity::getDduuid, ajGzddRequest.getId());
        List<GzddAjContentEntity> gzddAjContentEntityList = gzddAjContentService.list(queryGzddContentWrapper);
        int index = 0;
        if (Constants.GZDD_DD_PUSH.equals(type)) {
            for (GzddAjContentEntity gzddAjContentEntity : gzddAjContentEntityList) {
                if (TsBqUtils.isNull(gzddAjContentEntity.getAjcontent())) {
                    index += 1;
                }
            }
        }
        if (index == 0) {
            type = Constants.GZDD_DD_FINISH;
        }
        return type;
    }

    @ApiOperation(value = "工作调度-推送工作调度")
    @LogAnnotation(title = "工作调度", action = "推送工作调度")
    @PostMapping("/pushGzddwork")
    @ResponseBody
    @DS("oracle")
    public DataResult pushGzddwork(@RequestBody WorkflowLogEntity workflowLog) {
        String currentUserId = httpSessionService.getCurrentUserId();
        String id = workflowLog.getId();
        WorkflowLogEntity workflowLogEntity = workflowLogService.getById(id);
        workflowLogEntity.setUpdateId(currentUserId);
        //推送时间
//        workflowLogEntity.setUpdateTime(date);
        //已推送未办理
        workflowLogEntity.setType(Constants.GZDD_DD_PUSH);
        //推送给某单位
        String[] parts = workflowLog.getTaxorgcode().split("\\|");
        workflowLogEntity.setTaxorgcode(parts[0]);
        workflowLogService.updateById(workflowLogEntity);
        return DataResult.success();
    }

    @ApiOperation(value = "工作日志-科长审批")
    @LogAnnotation(title = "工作日志", action = "科长审批")
    @PostMapping("/addsectionchief")
    @RequiresPermissions("workflow:log:addsectionchief")
    @ResponseBody
    @DS("oracle")
    public DataResult addsectionchief(@RequestBody WorkflowLogEntity workflowLog) {
        String currentUserId = httpSessionService.getCurrentUserId();
        String id = workflowLog.getId();
        Date date = new Date();
        String sectionchiefContent = workflowLog.getSectionchiefContent();
        WorkflowLogEntity workflowLogEntity = workflowLogService.getById(id);
        workflowLogEntity.setSectionchief(currentUserId);
        workflowLogEntity.setSectionchiefContent(sectionchiefContent);
        workflowLogEntity.setSectionchiefDate(date);
        workflowLogEntity.setUpdateId(currentUserId);
        workflowLogEntity.setUpdateTime(date);
        workflowLogService.updateById(workflowLogEntity);
        return DataResult.success();
    }


    @ApiOperation(value = "工作调度-填写调度内容")
    @LogAnnotation(title = "工作调度", action = "填写调度内容")
    @PostMapping("/addGzddContent")
    @ResponseBody
    @DS("oracle")
    public DataResult addGzddContent(@RequestBody WorkflowLogEntity workflowLog) {
        String currentUserId = httpSessionService.getCurrentUserId();
        String id = workflowLog.getId();
        Date date = new Date();
        String sectionchiefContent = workflowLog.getSectionchiefContent();
        WorkflowLogEntity workflowLogEntity = workflowLogService.getById(id);
        workflowLogEntity.setSectionchief(currentUserId);
        workflowLogEntity.setSectionchiefContent(sectionchiefContent);
        workflowLogEntity.setSectionchiefDate(date);
        workflowLogEntity.setUpdateId(currentUserId);
        workflowLogEntity.setUpdateTime(date);
        workflowLogEntity.setType(Constants.GZDD_DD_FINISH);
        workflowLogService.updateById(workflowLogEntity);
        return DataResult.success();
    }

    @ApiOperation(value = "工作日志-纪检审批")
    @LogAnnotation(title = "工作日志", action = "纪检审批")
    @PostMapping("/adddiscipline")
    @RequiresPermissions("workflow:log:adddiscipline")
    @ResponseBody
    @DS("oracle")
    public DataResult adddiscipline(@RequestBody WorkflowLogEntity workflowLog) {
        String currentUserId = httpSessionService.getCurrentUserId();
        String id = workflowLog.getId();
        Date date = new Date();
        String disciplineContent = workflowLog.getDisciplineContent();
        WorkflowLogEntity workflowLogEntity = workflowLogService.getById(id);
        workflowLogEntity.setDiscipline(currentUserId);
        workflowLogEntity.setDisciplineContent(disciplineContent);
        workflowLogEntity.setDisciplineDate(date);
        workflowLogEntity.setUpdateId(currentUserId);
        workflowLogEntity.setUpdateTime(date);
        workflowLogService.updateById(workflowLogEntity);
        return DataResult.success();
    }

    @ApiOperation(value = "工作日志-查看")
    @LogAnnotation(title = "工作日志", action = "查看")
    @PostMapping("/view")
    @ResponseBody
    @DS("oracle")
    public DataResult view(@RequestBody WorkflowLogEntity workflowLog) {
        String id = workflowLog.getId();
        List<Object> list = new ArrayList<>();
        WorkflowLogEntity workflowLogEntity = workflowLogService.getById(id);
        list.add(workflowLogEntity);
        String relevance = workflowLogEntity.getRelevance();
        if (!TsBqUtils.isNull(relevance)) {
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            list.add(jcAjxxEntity);
        }

        String type = workflowLogEntity.getType();
        if (WorkflowConstants.LOG_TYPE_2.equals(type)) {
            // 查询模板
            LambdaQueryWrapper<WorkflowLogSurveyFeedbackEntity> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(WorkflowLogSurveyFeedbackEntity::getBatchId, id);
            List<WorkflowLogSurveyFeedbackEntity> list2 = workflowLogSurveyFeedbackService.list(queryWrapper);
            list.add(list2);
        }
        return DataResult.success(list);
    }


    @ApiOperation(value = "工作调度-查看调度模板内容")
    @LogAnnotation(title = "工作调度", action = "查看调度模板内容")
    @PostMapping("/getGzddContentView")
    @ResponseBody
    @DS("oracle")
    public DataResult getGzddContentView(@RequestBody WorkflowLogEntity workflowLog) {
        List<Object> list = new ArrayList<>();
        // 查询模板
        LambdaQueryWrapper<GzddmbFeedEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(GzddmbFeedEntity::getBatchId, workflowLog.getId());
        List<GzddmbFeedEntity> list2 = gzddmbFeedService.list(queryWrapper);
        list.add(list2);
        return DataResult.success(list);
    }

    @ApiOperation(value = "工作调度-查看填报调度内容")
    @LogAnnotation(title = "工作调度", action = "查看填报调度内容")
    @PostMapping("/getGzddTbView")
    @ResponseBody
    @DS("oracle")
    public DataResult getGzddTbView(@RequestBody AjGzddEntity ajGzddEntity) {
        String feelBack = ajGzddEntity.getAjContent();
        JSONArray feelBackJsonArray = JSON.parseArray(feelBack);
        List<GzddmbFeedEntity> gzddmbFeedEntityList = new ArrayList<>();
        for (Object o : feelBackJsonArray) {
            JSONObject optionsJsonObject = JSONObject.parseObject(o.toString());
            String optionsId = optionsJsonObject.get("optionsId").toString();
            String optionsType = optionsJsonObject.get("optionsType").toString();
            String optionsTitle = optionsJsonObject.get("optionsTitle").toString();
            String _feelBack = optionsJsonObject.get("feelBack").toString();
            // 保存服务端
            GzddmbFeedEntity gzddmbFeedEntity = new GzddmbFeedEntity();
            gzddmbFeedEntity.setId(optionsId);
            gzddmbFeedEntity.setOptionsId(optionsId);
            gzddmbFeedEntity.setOptionsTitle(optionsTitle);
            gzddmbFeedEntity.setOptionsFeedback(_feelBack);
            gzddmbFeedEntity.setOptionsType(optionsType);
            gzddmbFeedEntity.setOptionsRelation("");
            gzddmbFeedEntityList.add(gzddmbFeedEntity);
        }
        return DataResult.success(gzddmbFeedEntityList);
    }


    @ApiOperation(value = "工作调度-调度-案件列表")
    @LogAnnotation(title = "工作调度", action = "查看调度内容")
    @PostMapping("/queryGzddAjxxTable")
    @ResponseBody
    @DS("oracle")
    public DataResult queryGzddAjxxTable(@RequestBody WorkflowLogEntity workflowLog) {
        List<Object> list = new ArrayList<>();
        WorkflowLogEntity workflowLogEntity = workflowLogService.getById(workflowLog.getId());
        list.add(workflowLogEntity);
        String relevance = workflowLogEntity.getRelevance();
        if (!TsBqUtils.isNull(relevance)) {
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            list.add(jcAjxxEntity);
        }
        String type = workflowLogEntity.getType();
        if (Constants.GZDD_DD_PUSH.equals(type) || Constants.GZDD_DD_FINISH.equals(type)) {
            // 查询模板
            LambdaQueryWrapper<GzddmbFeedEntity> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(GzddmbFeedEntity::getBatchId, workflowLog.getId());
            List<GzddmbFeedEntity> list2 = gzddmbFeedService.list(queryWrapper);
            list.add(list2);
        }
        return DataResult.success(list);
    }

    // 科长查询
    @SuppressWarnings({"unchecked", "rawtypes"})
    @ApiOperation(value = "工作日志-科长查询")
    @LogAnnotation(title = "工作日志", action = "科长查询")
    @PostMapping("/querylist")
    @ResponseBody
    @DS("oracle")
    public DataResult querylist(@RequestBody WorkflowLogEntity workflowLog) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDeptId = httpSessionService.getCurrentDeptId();
        Page page = new Page(workflowLog.getPage(), workflowLog.getLimit());
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        if (!StringUtils.isEmpty(workflowLog.getStartTime())) {
            queryWrapper.gt(WorkflowLogEntity::getCreateTime, sdf.parse(workflowLog.getStartTime()));
        }
        if (!StringUtils.isEmpty(workflowLog.getEndTime())) {
            queryWrapper.lt(WorkflowLogEntity::getCreateTime, sdf.parse(workflowLog.getEndTime()));
        }
        String type = workflowLog.getType();
        if (!StringUtils.isEmpty(type)) {
            queryWrapper.eq(WorkflowLogEntity::getType, type);
        }
        queryWrapper.eq(WorkflowLogEntity::getTaxorgcode, currentDeptId);
        queryWrapper.orderByDesc(WorkflowLogEntity::getCreateTime);
        IPage<WorkflowLogEntity> iPage = workflowLogService.page(page, queryWrapper);
        List<WorkflowLogEntity> records = iPage.getRecords();
        for (WorkflowLogEntity workflowLogEntity : records) {
            String relevance = workflowLogEntity.getRelevance();
            if (TsBqUtils.isNotNull(relevance)) {
                JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
                if (TsBqUtils.isNotNull(jcAjxxEntity)) {
                    workflowLogEntity.setAjmc(jcAjxxEntity.getAjmc());
                    workflowLogEntity.setAjbh(jcAjxxEntity.getJcajbh());
                }
            }
            String createId = workflowLogEntity.getCreateId();
            SysUser sysUser = userService.getUser(createId);
            String realName = sysUser.getRealName();
            if (TsBqUtils.isNotNull(realName)) {
                workflowLogEntity.setCreateId(realName);
            }
            String type2 = workflowLogEntity.getType();
            if (type2.equals(WorkflowConstants.LOG_TYPE_2)) {
                // 处理案件日志，重新set CONTENT1
                String mold = workflowLogEntity.getMold();
                WorkflowLogSurveyEntity logSurvey = workflowLogSurveyService.getById(mold);
                if (TsBqUtils.isNotNull(logSurvey)) {
                    mold = logSurvey.getTitle();
                }
                workflowLogEntity.setContent1(mold);
            }
        }
        return DataResult.success(iPage);
    }

    // 局长查询
    @ApiOperation(value = "工作日志-局长查询")
    @LogAnnotation(title = "工作日志", action = "局长查询")
    @PostMapping("/querylist1")
    @ResponseBody
    @DS("oracle")
    public DataResult querylist1(@RequestBody WorkflowLogEntity workflowLog) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDeptId = httpSessionService.getCurrentDeptId();
        Page<WorkflowLogEntity> page = new Page<>(workflowLog.getPage(), workflowLog.getLimit());
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        if (!StringUtils.isEmpty(workflowLog.getStartTime())) {
            queryWrapper.between(WorkflowLogEntity::getCreateTime, sdf.parse(workflowLog.getStartTime()), sdf.parse(workflowLog.getEndTime()));
        }
        if (!StringUtils.isEmpty(workflowLog.getCreateName())) {
            queryWrapper.like(WorkflowLogEntity::getCreateName, workflowLog.getCreateName());
        }
        String type = workflowLog.getType();
        if (!StringUtils.isEmpty(type)) {
            queryWrapper.eq(WorkflowLogEntity::getType, type);
        }
        String _taxorgcode = workflowLog.getTaxorgcode();
        if (!StringUtils.isEmpty(_taxorgcode)) {
            queryWrapper.like(WorkflowLogEntity::getTaxorgcode, _taxorgcode);
        } else {
            queryWrapper.like(WorkflowLogEntity::getTaxorgcode, currentDeptId.substring(0, currentDeptId.length() - 2));
        }
        if (!StringUtils.isEmpty(workflowLog.getTaxPerson())) {
            queryWrapper.like(WorkflowLogEntity::getTaxPerson, workflowLog.getTaxPerson());
        }
        queryWrapper.in(WorkflowLogEntity::getType, "1", "2");
        queryWrapper.eq(WorkflowLogEntity::getDeleted, Constants.DELETEED_NOT);
        queryWrapper.orderByDesc(WorkflowLogEntity::getCreateTime);
        IPage<WorkflowLogEntity> iPage = workflowLogService.page(page, queryWrapper);
        List<WorkflowLogEntity> list = iPage.getRecords();
        for (WorkflowLogEntity workflowLogEntity : list) {
            String taxorgcode = workflowLogEntity.getTaxorgcode();
            DmGySwjgEntity swjgDm = dmGySwjgService.getSwjgDm(taxorgcode);
            //日志日期去掉最后一位
            Date contentdate = workflowLogEntity.getContentdate();
            workflowLogEntity.setContentdate(contentdate);
            if (TsBqUtils.isNotNull(swjgDm)) {
                String swjgmc = swjgDm.getSwjgmc();
                workflowLogEntity.setTaxorgcode(swjgmc);
            }
            String relevance = workflowLogEntity.getRelevance();
            if (TsBqUtils.isNotNull(relevance)) {
                JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
                if (TsBqUtils.isNotNull(jcAjxxEntity)) {
                    workflowLogEntity.setAjmc(jcAjxxEntity.getAjmc());
                    workflowLogEntity.setAjbh(jcAjxxEntity.getJcajbh());
                }
            }
            // 用户名称转义
            String createId = workflowLogEntity.getCreateId();
            String taxPerson = workflowLogEntity.getTaxPerson();
            if (TsBqUtils.isNotNull(createId)) {
                workflowLogEntity.setCreateId(createId + "|" + dmGySwryService.getById(createId).getSwryxm());
            } else {
                workflowLogEntity.setCreateId(createId);
            }
            if (TsBqUtils.isNotNull(taxPerson)) {
                if (taxPerson.length() > 11) {
                    workflowLogEntity.setTaxPerson(taxPerson);
                } else {
                    workflowLogEntity.setTaxPerson(taxPerson + "|" + dmGySwryService.getById(taxPerson).getSwryxm());
                }
            }
            String type2 = workflowLogEntity.getType();
            if (type2.equals(WorkflowConstants.LOG_TYPE_2)) {
                // 处理案件日志，重新set CONTENT1
                String mold = workflowLogEntity.getMold();
                WorkflowLogSurveyEntity logSurvey = workflowLogSurveyService.getById(mold);
                if (TsBqUtils.isNotNull(logSurvey)) {
                    mold = logSurvey.getTitle();
                }
                workflowLogEntity.setContent1(mold);
            }
        }
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "工作日志-局长查询(导出)")
    @LogAnnotation(title = "工作日志", action = "局长查询(导出)")
    @PostMapping("/exportQuerylist1")
    @ResponseBody
    @DS("oracle")
    public DataResult exportQuerylist1(@RequestBody WorkflowLogEntity workflowLog)
            throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDeptId = httpSessionService.getCurrentDeptId();
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        if (!StringUtils.isEmpty(workflowLog.getStartTime())) {
            queryWrapper.gt(WorkflowLogEntity::getCreateTime, sdf.parse(workflowLog.getStartTime()));
        }
        if (!StringUtils.isEmpty(workflowLog.getEndTime())) {
            queryWrapper.lt(WorkflowLogEntity::getCreateTime, sdf.parse(workflowLog.getEndTime()));
        }
        if (!StringUtils.isEmpty(workflowLog.getCreateName())) {
            queryWrapper.like(WorkflowLogEntity::getCreateName, workflowLog.getCreateName());
        }
        String type = workflowLog.getType();
        if (!StringUtils.isEmpty(type)) {
            queryWrapper.eq(WorkflowLogEntity::getType, type);
        }
        String _taxorgcode = workflowLog.getTaxorgcode();
        if (!StringUtils.isEmpty(_taxorgcode)) {
            queryWrapper.eq(WorkflowLogEntity::getTaxorgcode, _taxorgcode);
        }
        queryWrapper.like(WorkflowLogEntity::getTaxorgcode, currentDeptId.substring(0, 9));
        queryWrapper.orderByDesc(WorkflowLogEntity::getCreateTime);
        List<WorkflowLogEntity> list = workflowLogService.list(queryWrapper);
        for (WorkflowLogEntity workflowLogEntity : list) {
            String taxorgcode = workflowLogEntity.getTaxorgcode();
            DmGySwjgEntity swjgDm = dmGySwjgService.getSwjgDm(taxorgcode);
            if (TsBqUtils.isNotNull(swjgDm)) {
                String swjgmc = swjgDm.getSwjgmc();
                workflowLogEntity.setTaxorgcode(swjgmc);
            }
            String relevance = workflowLogEntity.getRelevance();
            if (TsBqUtils.isNotNull(relevance)) {
                JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
                if (TsBqUtils.isNotNull(jcAjxxEntity)) {
                    String ajmc = jcAjxxEntity.getAjmc();
                    workflowLogEntity.setAjmc(ajmc);
                }
            }

            // 用户名称转义
            String createId = workflowLogEntity.getCreateId();
            SysUser sysUser = userService.getUser(createId);
            String realName = createId;
            if (TsBqUtils.isNotNull(sysUser)) {
                realName = sysUser.getRealName();
            }
            if (TsBqUtils.isNotNull(realName)) {
                workflowLogEntity.setCreateId(realName);
            }
            String type2 = workflowLogEntity.getType();
            if (type2.equals(WorkflowConstants.LOG_TYPE_2)) {
                // 处理案件日志，重新set CONTENT1
                String mold = workflowLogEntity.getMold();
                WorkflowLogSurveyEntity logSurvey = workflowLogSurveyService.getById(mold);
                if (TsBqUtils.isNotNull(logSurvey)) {
                    mold = logSurvey.getTitle();
                }
                workflowLogEntity.setContent1(mold);
            }
        }

        if (!list.isEmpty()) {
            List<WorkflowLogExcel> workflowLogExcelList = new ArrayList<>();
            for (WorkflowLogEntity WorkflowLogEntity : list) {
                WorkflowLogExcel workflowLogExcel = WorkflowLogExcel.builder().id(WorkflowLogEntity.getId())
                        .createId(WorkflowLogEntity.getCreateName())
                        .type(Objects.equals(WorkflowLogEntity.getType(), "1") ? "日常日志" : "案件日志")
                        .taxorgcode(WorkflowLogEntity.getTaxorgcode()).contentdate(WorkflowLogEntity.getContentdate())
                        .content1(WorkflowLogEntity.getContent1())
                        .sectionchiefContent(WorkflowLogEntity.getSectionchiefContent())
                        .disciplineContent(WorkflowLogEntity.getDisciplineContent()).ajmc(WorkflowLogEntity.getAjmc())
                        .build();
                workflowLogExcelList.add(workflowLogExcel);
            }
            String fileName = TsBqUtils.getUUID() + ".xlsx";
            String excelPath = filePath + fileName;
            OutputStream out;
            try {
                out = new FileOutputStream(excelPath);
                ExcelWriter writer = EasyExcelFactory.getWriter(out);
                Sheet sheet1 = new Sheet(1, 0, WorkflowLogExcel.class);
                sheet1.setSheetName("数据");
                writer.write(workflowLogExcelList, sheet1);
                // 将上下文中的最终 outputStream 写入到指定文件中
                writer.finish();
                File excelFile = new File(excelPath);
                List<Object> excelFileInfo = new ArrayList<>();
                excelFileInfo.add(fileName);
                excelFileInfo.add(excelFile.length());
                excelFileInfo.add(fileUrl + "/" + fileName);
                return DataResult.success(excelFileInfo);
            } catch (FileNotFoundException e) {
                logger.error("xlsx导出处理时出错: {}", e.getMessage(), e);
                return DataResult.fail("xlsx生成失败：" + e);
            }
        } else {
            return DataResult.fail("传入的ids为null！");
        }
    }

    // 局长统计
    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "工作日志-局长统计")
    @LogAnnotation(title = "工作日志", action = "局长统计")
    @PostMapping("/statisticslist")
    @ResponseBody
    @DS("oracle")
    public DataResult statisticslist(@RequestBody WorkflowLogEntity workflowLog) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDeptId = httpSessionService.getCurrentDeptId();
        DmGySwjgEntity dmGySwjgEntity = dmGySwjgService.getSwjgDm(currentDeptId);
        String sjswjgDm = dmGySwjgEntity.getSjswjgDm();
        Page page = new Page(workflowLog.getPage(), workflowLog.getLimit());
        QueryWrapper<WorkflowLogEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<WorkflowLogEntity> lambdaQueryWrapper = queryWrapper.select(" taxorgcode," + " count(CASE"
                        + " WHEN type = '1' THEN" + " 1" + " ELSE" + " NULL" + "  END) AS TYPE1," + " count(CASE"
                        + " WHEN type = '2' THEN" + " 1" + " ELSE" + " NULL" + " END) AS TYPE2," + " count(1) AS COUNT")
                .lambda().groupBy(WorkflowLogEntity::getTaxorgcode);
        // 查询条件示例
        lambdaQueryWrapper.like(WorkflowLogEntity::getTaxorgcode, sjswjgDm.substring(0, workflowLog.getBz()));
        lambdaQueryWrapper.orderByAsc(WorkflowLogEntity::getTaxorgcode);
        if (!StringUtils.isEmpty(workflowLog.getStartTime())) {
            lambdaQueryWrapper.between(WorkflowLogEntity::getCreateTime, sdf.parse(workflowLog.getStartTime()), sdf.parse(workflowLog.getEndTime()));
        }
        IPage<WorkflowLogEntity> iPage = workflowLogService.page(page, queryWrapper);
        List<WorkflowLogEntity> records = iPage.getRecords();
        for (WorkflowLogEntity workflowLogEntity : records) {
            String _taxorgcode = workflowLogEntity.getTaxorgcode();
            DmGySwjgEntity _dmGySwjgEntity = dmGySwjgService.getSwjgDm(_taxorgcode);
            String swjgmc = _dmGySwjgEntity.getSwjgmc();
            workflowLogEntity.setTaxorg(swjgmc);
        }
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "工作日志-市级统计")
    @LogAnnotation(title = "工作日志", action = "市级统计")
    @PostMapping("/statisticsSjlist")
    @ResponseBody
    @DS("oracle")
    public DataResult statisticsSjlist(@RequestBody WorkflowLogEntity workflowLog) throws ParseException {
        String startTime = workflowLog.getStartTime();
        String endTime = workflowLog.getEndTime();
        List<WorkflowLogEntity> workflowLogEntityList = workflowLogMapper.querySjRztj(startTime, endTime);
        return DataResult.success(workflowLogEntityList);
    }

    @SuppressWarnings("unchecked")
    @ApiOperation(value = "工作日志-工作查询2")
    @LogAnnotation(title = "工作日志", action = "工作查询2")
    @PostMapping("/querylist2")
    @ResponseBody
    @DS("oracle")
    public DataResult querylist2(@RequestBody WorkflowLogEntity workflowLog) {
        String ajbh = workflowLog.getAjbh();
        if (TsBqUtils.isNull(ajbh)) {
            return DataResult.fail("案件编号为输入，请输入案件编号！");
        }
        JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajbh(ajbh);
        if (TsBqUtils.isNull(jcAjxxEntity)) {
            return DataResult.fail("输入的案件编号未查询到案件信息！");
        }
        String jcajxxuuid = jcAjxxEntity.getJcajxxuuid();
        @SuppressWarnings("rawtypes")
        Page page = new Page(workflowLog.getPage(), workflowLog.getLimit());
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkflowLogEntity::getRelevance, jcajxxuuid);
        IPage<WorkflowLogEntity> iPage = workflowLogService.page(page, queryWrapper);
        List<WorkflowLogEntity> records = iPage.getRecords();
        for (WorkflowLogEntity workflowLogEntity : records) {
            String type2 = workflowLogEntity.getType();
            if (type2.equals(WorkflowConstants.LOG_TYPE_2)) {
                // 处理案件日志，重新set CONTENT1
                String mold = workflowLogEntity.getMold();
                WorkflowLogSurveyEntity logSurvey = workflowLogSurveyService.getById(mold);
                if (TsBqUtils.isNotNull(logSurvey)) {
                    mold = logSurvey.getTitle();
                }
                workflowLogEntity.setContent1(mold);
            }
        }
        return DataResult.success(iPage);
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    @ApiOperation(value = "工作日志-工作查询2")
    @LogAnnotation(title = "工作日志", action = "通过人员代码 ")
    @PostMapping("/querylistByCreateId")
    @ResponseBody
    @DS("oracle")
    public DataResult querylistByCreateId(@RequestBody WorkflowLogEntity workflowLog) {
        String createId = workflowLog.getCreateId();
        Page page = new Page(workflowLog.getPage(), workflowLog.getLimit());
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkflowLogEntity::getCreateId, createId);
        queryWrapper.orderByDesc(WorkflowLogEntity::getCreateTime);
        IPage<WorkflowLogEntity> iPage = workflowLogService.page(page, queryWrapper);
        List<WorkflowLogEntity> records = iPage.getRecords();
        for (WorkflowLogEntity workflowLogEntity : records) {
            String type2 = workflowLogEntity.getType();
            if (type2.equals(WorkflowConstants.LOG_TYPE_2)) {
                // 处理案件日志，重新set CONTENT1
                String mold = workflowLogEntity.getMold();
                WorkflowLogSurveyEntity logSurvey = workflowLogSurveyService.getById(mold);
                if (TsBqUtils.isNotNull(logSurvey)) {
                    mold = logSurvey.getTitle();
                }
                workflowLogEntity.setContent1(mold);
            }
        }
        return DataResult.success(iPage);
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    @ApiOperation(value = "工作调度-导入案件编号")
    @LogAnnotation(title = "工作调度", action = "导入案件编号")
    @PostMapping("/uploadGzddExcel")
    @ResponseBody
    @DS("oracle")
    public DataResult uploadGzddExcel(@RequestParam("file") MultipartFile file) {
        // 读取上传的 Excel 文件
        if (file.isEmpty()) {
            return DataResult.fail("文件为空");
        }
        try (InputStream inputStream = file.getInputStream()) {
            // 使用 Apache POI 读取 Excel 文件
            Workbook workbook = new XSSFWorkbook(inputStream);
            org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
            List<String> caseNumbers = new ArrayList<>();
            // 遍历每一行（从第二行开始，跳过表头）
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                if (row == null) continue;
                // 获取第一列的值
                Cell cell = row.getCell(0);
                if (cell != null) {
                    String caseNumber = cell.getStringCellValue().trim();
                    if (!caseNumber.isEmpty()) {
                        caseNumbers.add(caseNumber);
                    }
                }
            }
            JcAjxxEntity jcAjxxEntity = new JcAjxxEntity();
            IPage page = new Page(jcAjxxEntity.getPage(), jcAjxxEntity.getLimit());
            LambdaQueryWrapper<JcAjxxEntity> queryWrapper = Wrappers.lambdaQuery();
            //根据Jcajxxuuid查询案件信息
            queryWrapper.in(JcAjxxEntity::getJcajbh, caseNumbers);
            IPage<JcAjxxEntity> iPage = this.jcAjxxService.page(page, queryWrapper);
            List<JcAjxxEntity> jcAjxxEntityList = iPage.getRecords();
            for (JcAjxxEntity ajxxEntity : jcAjxxEntityList) {
                String ajjczt = ajxxEntity.getAjjcztDm();
                DmJcAjztEntity dmJcAjzt = dmJcAjztService.getByAjztDm(ajjczt);
                if (TsBqUtils.isNotNull(dmJcAjzt)) {
                    ajjczt = dmJcAjzt.getAjztmc();
                }
                ajxxEntity.setAjjcztDm(ajjczt);
            }
            return DataResult.success(iPage);
        } catch (IOException e) {
            logger.error("文件解析失败", e);
            return DataResult.fail("文件解析失败");
        }
    }

    @ApiOperation(value = "检举案件-一键调度")
    @LogAnnotation(title = "公用", action = "一键调度")
    @PostMapping("/gyYjdd")
    @ResponseBody
    @DS("oracle")
    public DataResult gyYjdd(@RequestBody AjGzddEntity ajGzddEntity) throws ParseException {
        boolean relust = workflowLogService.saveYjdd(ajGzddEntity);
        return DataResult.success(relust);
    }

}